package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.Product;
import com.ysd.util.DBManager;

public class ProductDetailsDaoImpl {

	public List<Product> getroductDetails(int cId) {
		List<Product> ProductDetailsList = new ArrayList<Product>();
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {// 正常
				// 2.创建Statement对象
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * from product where productId=" + cId;
			// 4.执行sql语句
			rs = st.executeQuery(sql);// execute执行jQuery查询
			// 5.遍历结果集
			while (rs.next()) {
				int productId = rs.getInt("productId");
				String productName = rs.getString("productName");
				int marketPrice = rs.getInt("marketPrice");
				String brand = rs.getString("brand");
				String isDistribution = rs.getString("isDistribution");
				String isCashPayment = rs.getString("isCashPayment");
				String flavor = rs.getString("flavor");
				String productImg = rs.getString("productImg");
				int productCategoryId = rs.getInt("productCategoryId");
				Product productDetails = new Product(productId, productName, marketPrice, brand, isDistribution, isCashPayment, flavor, productImg, productCategoryId);
				ProductDetailsList.add(productDetails);
			}
			return ProductDetailsList;// 正常执行返回用户集合
		} catch (SQLException e) {// 异常
			e.printStackTrace();
			return null;// 异常执行返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 显示全部产品信息
	 * @return
	 */
	public ArrayList<Product> getProductXinXi() {
		ArrayList<Product> productList = new ArrayList<Product>();
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {// 正常
				// 2.创建Statement对象
			st = conn.createStatement();
			// 3.sql语句
			String sql = "select * from product";
			// 4.执行sql语句
			rs = st.executeQuery(sql);// execute执行jQuery查询
			// 5.遍历结果集
			while (rs.next()) {
				int productId = rs.getInt("productId");
				String productName = rs.getString("productName");
				int marketPrice = rs.getInt("marketPrice");
				String brand = rs.getString("brand");
				String isDistribution = rs.getString("isDistribution");
				String isCashPayment = rs.getString("isCashPayment");
				String flavor = rs.getString("flavor");
				String productImg = rs.getString("productImg");
				int productCategoryId = rs.getInt("productCategoryId");
				Product productDetails = new Product(productId, productName, marketPrice, brand, isDistribution, isCashPayment, flavor, productImg, productCategoryId);
				productList.add(productDetails);
			}
			return productList;// 正常执行返回用户集合
		} catch (SQLException e) {// 异常
			e.printStackTrace();
			return null;// 异常执行返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
}
